package 剑指offer;


/*
 * Author：江松
 * Date：2023/2/20 17:36
 *
 *
 * 链表逆序打印
 */

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main3 {
      class ListNode{
        int val;
        ListNode next;
    }

    public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
         ArrayList<Integer> ans=new ArrayList<>();
        ListNode node=listNode;
        while(node!=null){
            ans.add(0,node.val);
            node=node.next;
        }
        return ans;
    }

    public  ListNode create(){
         ListNode head=new ListNode();
         int n=sc.nextInt();
         ListNode p,q;
         q=head;
         while (n--!=0){
             int v=sc.nextInt();
             p=new ListNode();
             p.next=null;
             p.val=v;
             q.next=p;
             q=p;
         }
         return head.next;
    }

    public void print(ListNode listNode){
          ListNode node=listNode;
          while(node!=null){
              System.out.println(node.val);
              node=node.next;
          }
    }

     static Scanner sc=new Scanner(System.in);
    public static void main(String[] args) {
        Main3 main3=new Main3();
        ListNode listNode=main3.create();
        //main3.print(listNode);
        ArrayList<Integer>ans=main3.printListFromTailToHead(listNode);
        System.out.println(ans);
    }
}
